﻿using System;
using System.Collections.Generic;

using JetBrains.Annotations;

using octalforty.Componento.Core.Framework;
using octalforty.Componento.Core.ObjectModel;

using System.Linq;

using octalforty.Componento.Core.Util;

namespace octalforty.Componento.Core.ApplicationServices
{
    public class PackageRepositoryBrowserService
    {
        private readonly IEnumerable<IPackageRepositoryBrowser> packageRepositoryBrowsers;

        public PackageRepositoryBrowserService(IEnumerable<IPackageRepositoryBrowser> packageRepositoryBrowsers)
        {
            this.packageRepositoryBrowsers = packageRepositoryBrowsers;
        }

        public virtual IEnumerable<PackageManifest> GetPackageManifests([NotNull] Uri repositoryUri, [NotNull] string name, [CanBeNull] PackageSpecification specification)
        {
            var browser = packageRepositoryBrowsers.First(prb => prb.SupportedSchemas.Contains(repositoryUri.Scheme.ToLowerInvariant()));
            return browser == null ? 
                null :
                browser.GetPackageManifests(repositoryUri, name, specification);
        }

        public virtual PackageManifest GetPackageManifest([NotNull] Uri repositoryUri, [NotNull] string name, [CanBeNull] PackageSpecification specification)
        {
            var browser = packageRepositoryBrowsers.First(prb => prb.SupportedSchemas.Contains(repositoryUri.Scheme.ToLowerInvariant()));
            return browser == null ? 
                null :
                browser.GetPackageManifest(repositoryUri, name, specification);
        }
    }
}
